package com.lucifer.starter.server;

import com.lucifer.core.exception.ServiceException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by tangdaoyuan on 2017/1/9.
 */
@ControllerAdvice(basePackages = {"com.lucifer"}, assignableTypes = {AbstractController.class})
public class GlobeExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    ResponseEntity handleControllerException(HttpServletRequest request, ServiceException ex) {
        return new ResponseEntity(ex.get().json(), HttpStatus.valueOf(400));
    }
}
